package com.xcc.dataStructures.demo14_algapplication;

/**
 * 暴力递归
 */
public class Demo04_ViolenceMatch {

    public static void main(String[] args) {
        String str1 = "abcabcabcabca";
        String str2 = "abaa";
        int index = violenceMatch(str1,str2);
        System.out.println(index);
    }

    /**
     * 暴力匹配
     * @param str1  原始字符串
     * @param str2  匹配字符串
     */
    public static int violenceMatch(String str1,String str2) {
        //表示字符串str2的匹配的索引位置
        int j;
        for (int i = 0; i < str1.length();) {
            j = 0;
            while (i < str1.length() && j < str2.length() && str1.charAt(i) == str2.charAt(j)) {
                i++;
                j++;
            }
            //将j匹配到最后一个字符
            if (j==str2.length()) {
                return i-j;
            }
            i = i - j + 1;
        }
        return -1;
    }

}
